/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator.pages;

import net.db.generator.field.models.BaseFieldModel;
import net.db.generator.utils.ClassGeneratorUtil;

/**
 *
 * @author abosancic
 */
public class JSFViewWriter extends JSFBaseWriter{

    public JSFViewWriter() {
        fileName = "View";
    }

    @Override
    protected void generateDefineTitle() {
        stringXHTML.append(spaces).append("<ui:define name=\"title\">\n")
                .append(spaces).append(spaces).append("<h:outputText value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "Title}\"></h:outputText>\n")
                .append(spaces).append("</ui:define>").append("\n");
        
        properties.put("View" + ClassGeneratorUtil.humanize(className) + "Title", "View");
    }
    
    @Override
    protected void generateFields() {
        StringBuilder toStringMethod = new StringBuilder();
        
        for (BaseFieldModel fieldModel : fields) {
            String field = fieldModel.getFieldName();
            field = ClassGeneratorUtil.humanizeToField(fieldModel.getFieldName());
            stringXHTML.append(spaces).append(spaces).append("<h:outputText value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "Label_" + field + "}\" />\n")
                       .append(spaces).append(spaces).append("<h:outputText id=\"" + field + "\" value=\"#{"  + ClassGeneratorUtil.humanizeToField(className) +  "Controller.selected."  + field +  "}\" title=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "Title_" + field + "}\"/>");
            stringXHTML.append("<br />");
            stringXHTML.append("\n");
            
            // add Bundle
            properties.put("View" + ClassGeneratorUtil.humanize(className) + "Label_" + field, ClassGeneratorUtil.humanize(field) + ":");
            properties.put("View" + ClassGeneratorUtil.humanize(className) + "Title_" + field, ClassGeneratorUtil.humanize(field));
        }
        
        stringXHTML.append(spaces).append("\n");
    }
    
    @Override
     protected void generateEndForm() {
        stringXHTML.append(spaces).append(" </h:panelGroup>\n")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.destroyAndView}\" "
                + "value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "DestroyLink}\" />\n")
                .append(spaces).append(spaces).append("<br />\n")
                .append(spaces).append(spaces).append("<br />\n")
                .append(spaces).append("<h:link outcome=\"Edit\" value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "EditLink}\"/>\n")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareCreate}\" value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "CreateLink}\" immediate=\"true\"/>")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:commandLink action=\"#{" + ClassGeneratorUtil.humanizeToField(className) + "Controller.prepareList}\" value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "ShowAllLink}\" immediate=\"true\"/>")
                .append(spaces).append("<br />\n")
                .append(spaces).append("<h:link outcome=\"/index\" value=\"#{bundle.View" + ClassGeneratorUtil.humanize(className) + "IndexLink}\" />")
                .append(spaces).append("</h:form>\n");
    }
    
    @Override
    protected void addInitProperties() {
        String name = ClassGeneratorUtil.humanize(className);
        properties.put("View" + name + "DestroyLink", "Destroy");
        properties.put("View" + name + "EditLink", "Edit");
        properties.put("View" + name + "CreateLink", "Create New " + name);
        properties.put("View" + name + "ShowAllLink", "Show All " + name + " Items");
        properties.put("View" + name + "IndexLink", "Index");
    }
    
}
